<template>
  <div class="emergency app-container" style="height: 100%; padding: 0">
    <div class="container-box">
      <div class="right-container">
        <el-row>
          <el-col :span="4">
            <el-button v-show="isShowBack" icon="el-icon-back" size="small" @click="$router.go(-1)">返回</el-button>
          </el-col>
        </el-row>
        <!-- 搜索框 -->
        <search-form
          :data="queryParams"
          :field-list="filterInfo.fieldList"
          @handleSearch="handleQuery"
          @handleReset="resetQuery"
        />
        <!--    新增按钮    -->
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增 </el-button>
            <el-button size="mini" :disabled="multiple" @click="handleDelBatch">批量删除</el-button>
          </el-col>
        </el-row>
        <!--    列表    -->
        <hk-table
          ref="tables"
          :loading="loading"
          :table-data="tableData"
          :table-columns="tableFieldList"
          :total="total"
          :selection="true"
          :show-pag="true"
          :operate="true"
          :table-height="80"
          :query-params="queryParams"
          :show-index="true"
          @changePage="changePage"
          @handleSelectionChange="handleSelectionChange"
        >
          <!--    操作   -->
          <template #operate="scope">
            <el-button size="mini" type="text" @click="handleUpdate(scope.row, scope.index)">编辑 </el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.row, scope.index)">删除 </el-button>
          </template>
          <!--     启用状态     -->
          <template #useStatus="scope">
            <el-switch
              v-model="scope.row.useStatus"
              :active-value="1"
              :inactive-value="0"
              @change="handleChange($event, scope.row)"
            />
          </template>
        </hk-table>
        <konwledge-dia ref="k_dia" :title="title" :form="formData" @handleSave="getList" />
      </div>
    </div>
  </div>
</template>

<script>
import HkTable from '@/components/Hk-Table'
import SearchForm from '@/components/HkSearchform'
import { mapMutations, mapState } from 'vuex'
import KonwledgeDia from './add'
import { tableList, editState, deleteBatch, emergencyPlanDelete } from '@/api/dispose/emergencyResponsePlan'

export default {
  components: { KonwledgeDia, HkTable, SearchForm },
  data() {
    return {
      open: false,
      title: '',
      // 总条数
      total: 0,
      // 是否在加载
      loading: true,
      tableData: [],
      // 表格数据
      tableFieldList: [
        { label: '名称', param: 'processName' },
        { label: '关联事件', param: 'kpiName' },
        { label: '创建时间', param: 'createTime', width: '200' },
        { label: '更新时间', param: 'updateTime', width: '200' },
        { label: '启用状态', param: 'useStatus', width: '100', type: 'slot' }
      ],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        keyWord: undefined
      },
      filterInfo: {
        fieldList: [{ label: '关键字', field: 'keyWord', type: 'input' }]
      },
      ids: [],
      multiple: true,
      formData: {}
    }
  },
  methods: {
    ...mapMutations({ CHANGE_SHOWBACK: 'portrait/CHANGE_SHOWBACK' }),
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true
      this.title = '添加'
      this.formData = {}
      this.$refs.k_dia.show()
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true
      tableList(this.queryParams).then((res) => {
        this.loading = false
        this.tableData = res.list
        this.total = res.total
      })
    },
    /** 搜索按钮操作 */
    handleQuery(data) {
      this.queryParams.current = 1
      this.queryParams.keyWord = data.keyWord
      this.getList()
    },
    /** 重置按钮 */
    resetQuery() {
      this.queryParams = { size: 10, current: 1 }
      this.getList()
    },
    /** 启用状态按钮切换 */
    handleChange(value, scope) {
      editState({ id: scope.id, useStatus: value })
        .then((res) => {
          this.$message.success('状态修改成功')
          this.getList()
        })
        .catch((error) => {})
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.title = '编辑'
      this.$refs.k_dia.show(row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.processName + '"的数据项？')
        .then(function() {
          return emergencyPlanDelete({ id: row.id })
        })
        .then((res) => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 批量删除按钮 */
    handleDelBatch() {
      const ids = this.ids.join(',')
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          return deleteBatch({ ids: ids })
        })
        .then(() => {
          this.$message.success('删除成功')
          this.getList()
        })
        .catch((error) => {
          console.log(error)
        })
    },
    /** 分页 */
    changePage(data) {
      this.getList()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.multiple = !selection.length
    }
  },
  destroyed() {
    this.CHANGE_SHOWBACK(false)
  },
  computed: {
    ...mapState({
      isShowBack: (state) => {
        state.portrait.isShowBack || false
      }
    })
  },
  mounted() {
    this.getList()
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  .targetTable {
    flex: 1;
    overflow: hidden;
  }
}
.right-container {
  height: 100%;
  display: flex;
  padding: 0 20px 20px !important;
  flex-direction: column;
  .el-table {
    flex: 1;
  }
}
</style>
